using System;
using Microsoft.Practices.ObjectBuilder2;

namespace Microsoft.Practices.Unity.Interception
{
    public class RemotingInterceptionStrategy : BuilderStrategy
    {
        public override void PreBuildUp(IBuilderContext context)
        {
            Type typeToBuild;

            if (BuildKey.TryGetType(context.BuildKey, out typeToBuild))
            {
                Type originalType;
                if (!BuildKey.TryGetType(context.OriginalBuildKey, out originalType))
                    originalType = typeToBuild;

                IRemotingInterceptionPolicy policy = context.Policies.Get<IRemotingInterceptionPolicy>(context.BuildKey);

                if (context.Existing != null && policy != null)
                    context.Existing = RemotingInterceptor.Wrap(context.Existing, originalType, policy);
            }

            base.PreBuildUp(context);
        }
    }
}